package com.tuyue.police.biz.impl;

import com.tuyue.police.bean.DeviceBean;
import com.tuyue.police.bean.EnterpriseBean;
import com.tuyue.police.biz.IEnterpriseBiz;
import com.tuyue.police.dao.IDeviceDao;
import com.tuyue.police.dao.IEnterpriseDao;
import com.tuyue.police.dao.ISectorDao;
import com.tuyue.police.dao.IUserDao;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.Device;
import com.tuyue.police.pojo.Enterprise;
import com.tuyue.police.pojo.Sector;
import com.tuyue.police.pojo.User;
import com.tuyue.police.util.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/14.
 * @Modified By:
 */
@Service
public class EnterpriseBizImpl implements IEnterpriseBiz {

    @Autowired
    private IEnterpriseDao enterpriseDao;
    @Autowired
    private IUserDao userDao;
    @Autowired
    private IDeviceDao deviceDao;
    @Autowired
    private ISectorDao sectorDao;
    @Override
    public Enterprise addAndUpdata(Enterprise enterprise) {
        User one = userDao.findOne(enterprise.geteUserId());
        if (ObjectUtils.isEmpty(one)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        enterprise.seteOrderType((byte) 0);
        enterprise.seteAuthenticationType((byte) 0);
        return enterpriseDao.save(enterprise);
    }

    @Override
    public Page<Enterprise> findAllByUserId(Long userId,Pageable pageable) {
        User one = userDao.findOne(userId);
        if (ObjectUtils.isEmpty(one)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        return enterpriseDao.findAllByEUserId(userId,pageable);
    }

    @Override
    public List<Enterprise> findAllByUserIdList(Long userId) {
        User one = userDao.findOne(userId);
        if (ObjectUtils.isEmpty(one)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        return enterpriseDao.findEnterpriseLit(userId);
    }

    @Override
    public List<EnterpriseBean> findTreeByUserId(Long userId) {
        User one = userDao.findOne(userId);
        if (ObjectUtils.isEmpty(one)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        List<EnterpriseBean> enterpriseBeans=new ArrayList<>();
        List<Enterprise> enterprises = enterpriseDao.findEnterpriseLit(userId);
        if (enterprises.size()>0) {
            for (Enterprise enterpris : enterprises) {
                EnterpriseBean enterpriseBean = new EnterpriseBean();
                BeanUtils.copyProperties(enterpris,enterpriseBean);
                List<Device> devices = deviceDao.findAllByDeviceEnterpriseId(enterpris.geteId());
                if (devices.size()>0) {
                    List<DeviceBean> deviceBeans = new ArrayList<>();
                    for (Device device : devices) {
                        DeviceBean deviceBean = new DeviceBean();
                        BeanUtils.copyProperties(device,deviceBean);
                        List<Sector> sectors = sectorDao.findAllBySectorDeviceId(device.getDeviceId());
                        deviceBean.setSectors(sectors);
                        deviceBeans.add(deviceBean);
                    }
                    enterpriseBean.setDeviceBeans(deviceBeans);
                    enterpriseBeans.add(enterpriseBean);
                }
            }
        }
        return enterpriseBeans;
    }

    @Override
    public Enterprise findOne(Long eId) {
        Enterprise one = enterpriseDao.findOne(eId);
        if (ObjectUtils.isEmpty(one)) {
            throw new MyException(ResultEnum.ENTERPRISE_ERROR);
        }
        return one;
    }

    @Override
    public Enterprise uploadImg(Long eid, byte[] file,String filePath, String fileName,String imgAddress) {
        Enterprise enterprise = enterpriseDao.findOne(eid);
        if (enterprise==null) {
            throw new MyException(ResultEnum.ENTERPRISE_ERROR);
        }
        try {
            String s = FileUtil.uploadFile(file, filePath, fileName,imgAddress);
            enterprise.seteImgLocation(s);
        } catch (Exception e) {
            throw new MyException(ResultEnum.NOFILE_ERROR);
        }
        return enterpriseDao.save(enterprise);
    }
}
